package com.douguo.recipe.network;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;

import com.douguo.recipe.RecipelistActivity;
import com.douguo.recipe.object.Caipin;
import com.douguo.recipe.object.Caipu;
import com.douguo.recipe.object.Caixi;
import com.douguo.recipe.object.Kouwei;
import com.douguo.recipe.object.Nandu;
import com.douguo.recipe.object.Sort;
import com.douguo.recipe.object.Tese;

public class GetSort {
	private static final String TAG = "GetSort";

	
	
	//read raw data,while inputstream is not null 
	public static void getSort(final InputStream inputstream, final Handler handler) {
		final List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Runnable runnable;
		if (inputstream != null) {
			runnable = new Runnable() {
				
				public void run() {
					List<Sort> sorts = (List<Sort>) mParseListener.onParsing(inputstream);
					handler.sendMessage(handler.obtainMessage(
							Constant.GET_SORT, sorts));
					
				}
			};
			
		} else {
			runnable = new Runnable() {

				public void run() {
					DouguoConnection mConn = new DouguoConnection(
							Constant.URL_GET_SORT_LIST, nvps, mParseListener,
							DouguoConnection.GET_METHOD);
					List<Sort> recipes = (List<Sort>) mConn.Connect();
					handler.sendMessage(handler.obtainMessage(
							Constant.GET_SORT, recipes));

				}

			};
			
		}
		new Thread(runnable).start();
	}
	


	private static OnParseListener mParseListener = new OnParseListener() {

		public Object onParsing(InputStream is) {

			XmlPullParser parser = Xml.newPullParser();
			List<Sort> sorts = null;
			Sort sort = null;
			boolean done = false;

			try {

				parser.setInput(is, null);
				int eventType = parser.getEventType();
				while (eventType != XmlPullParser.END_DOCUMENT && !done) {
					String name = null;
					switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
						sorts = new ArrayList<Sort>();
						break;
					case XmlPullParser.START_TAG:
						name = parser.getName();
						if (name.equalsIgnoreCase("caixi")) {
							sort = new Caixi(parser.getAttributeValue(null,
									"id"), parser.getAttributeValue(null,
									"name"));
							sorts.add(sort);

						} else if (name.equalsIgnoreCase("caipin")) {
							sort = new Caipin(parser.getAttributeValue(null,
									"id"), parser.getAttributeValue(null,
									"name"));
							sorts.add(sort);

						} else if (name.equalsIgnoreCase("caipu")) {
							sort = new Tese(
									parser.getAttributeValue(null, "id"),
									parser.getAttributeValue(null, "name"));
							sorts.add(sort);

						} else if (name.equalsIgnoreCase("nandu")) {
							sort = new Nandu(parser.getAttributeValue(null,
									"id"), parser.getAttributeValue(null,
									"name"));
							sorts.add(sort);

						} else if (name.equalsIgnoreCase("kouwei")) {
							sort = new Kouwei(parser.getAttributeValue(null,
									"id"), parser.getAttributeValue(null,
									"name"));
							sorts.add(sort);

						}

						break;

					case XmlPullParser.END_DOCUMENT:
						done = true;
						break;
					}
					eventType = parser.next();
				}
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return sorts;
		}

	};
}
